package com.systop.tea.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.systop.tea.common.R;
import com.systop.tea.dto.ProductDto;
import com.systop.tea.entity.Banner;
import com.systop.tea.entity.Category;
import com.systop.tea.entity.Product;
import com.systop.tea.service.CategoryService;
import com.systop.tea.service.ProductService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequiredArgsConstructor
@RequestMapping("/product")
public class ProductController {

    private final ProductService productService;
    private final CategoryService categoryService;

    @GetMapping("/query")
    public R<Page> query(Product product, Integer pageNum, Integer pageSize) {
        System.out.println(product);
        Page<Product> pageInfo = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Product> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(product.getProductName()), Product::getProductName, product.getProductName());
        queryWrapper.eq(product.getCategoryId() != null, Product::getCategoryId, product.getCategoryId());
        queryWrapper.eq(product.getStatus() != null, Product::getStatus, product.getStatus());
        queryWrapper.orderByDesc(product.getSales() != null, Product::getSales);
        productService.page(pageInfo, queryWrapper);

        Page<ProductDto> productDtoPage = new Page<>();
        BeanUtils.copyProperties(pageInfo, productDtoPage, "records");

        List<Product> records = pageInfo.getRecords();
        List<ProductDto> list = records.stream().map((item) -> {
            ProductDto productDto = new ProductDto();
            BeanUtils.copyProperties(item, productDto);
            Category category = categoryService.getById(item.getCategoryId());
            if (category != null)
                productDto.setCategoryName(category.getCategoryName());
            return productDto;
        }).collect(Collectors.toList());

        productDtoPage.setRecords(list);
        return R.success(productDtoPage);
    }

    @GetMapping("/{id}")
    public R<ProductDto> queryById(@PathVariable Integer id) {
        Product product = productService.getById(id);
        Category category = categoryService.getById(product.getCategoryId());
        ProductDto productDto = new ProductDto();

        BeanUtils.copyProperties(product, productDto);
        productDto.setCategoryName(category.getCategoryName());
        return R.success(productDto);
    }

    @PostMapping
    public R<String> add(@RequestBody Product product) {
        product.setAddTime(LocalDateTime.now());
        boolean row = productService.save(product);
        if (row) {
            return R.success("商品添加成功");
        } else {
            return R.error("商品添加失败");
        }
    }

    @PutMapping
    public R<String> modify(@RequestBody Product product) {
        boolean row = productService.updateById(product);
        if (row) {
            return R.success("商品修改成功");
        } else {
            return R.error("商品修改失败");
        }
    }

    @DeleteMapping("/{id}")
    public R<String> delete(@PathVariable Integer id) {
        boolean rows = productService.removeById(id);
        if (rows) {
            return R.success("商品删除成功");
        } else {
            return R.error("商品删除失败");
        }
    }

}
